package org.example.consumer.controller;

import org.example.consumer.component.ServiceURL;
import org.example.utils.ResultTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/hotel")
public class HotelController {
    private static final String REQUEST_URL = "hotel/";
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private ServiceURL serviceURL;

    @GetMapping(value = "/queryAll")
    public Object queryAll(Integer pageNum,String city){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("pageNum",pageNum);
        paramMap.put("city",city);
        return restTemplate.getForObject(serviceURL.getPlatfromUrl()+REQUEST_URL+"queryAll",ResultTemplate.class,paramMap);
    }

    @GetMapping(value = "/hotelRecommend")
    public Object queryRecommend(String city){
        return restTemplate.getForObject(serviceURL.getPlatfromUrl()+REQUEST_URL+"hotelRecommend?city="+city,ResultTemplate.class);
    }

    @PostMapping(value = "/hotelSearchBySolr")
    public Object queryHotelSearchSolr(String hotelName,String cityName){
        System.out.println(serviceURL.getAuthUrl()+REQUEST_URL+"hotelSearchBySolr");
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("hotelName", hotelName);
        paramMap.add("cityName", cityName);
        return restTemplate.postForObject(serviceURL.getPlatfromUrl()+REQUEST_URL+"hotelSearchBySolr",paramMap,ResultTemplate.class);
    }

    @GetMapping(value = "/hotelDetail")
    public Object queryHotelDetail(Long id){
        return restTemplate.getForObject(serviceURL.getPlatfromUrl()+REQUEST_URL+"hotelDetail?id="+id,ResultTemplate.class);
    }
}
